package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.dto.StudyNoteDTO;
import com.example.computer_exam_training_sys.service.UserStudyNotesService;
import com.example.computer_exam_training_sys.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/api/notes")
public class UserStudyNotesController {

    @Autowired
    private UserStudyNotesService service;

    // 获取用户的笔记列表
    @GetMapping("/user/{userId}")
    public Result<List<StudyNoteDTO>> getUserNotes(@PathVariable Long userId) {
        return Result.success(service.getUserNotes(userId));
    }

    // 创建笔记
    @PostMapping("/createNote")
    public Result<StudyNoteDTO> createNote(@RequestBody Map<String, Object> params) {
        Long userId = Long.valueOf(params.get("userId").toString());
        Long questionId = Long.valueOf(params.get("questionId").toString());
        String content = (String) params.get("content");
        return Result.success(service.createNote(userId, questionId, content));
    }

    // 更新笔记
    @PutMapping("update/{noteId}")
    public Result<StudyNoteDTO> updateNote(
            @PathVariable Long noteId,
            @RequestBody Map<String, Object> params) {
        String content = (String) params.get("content");
        Long userId = Long.valueOf(params.get("userId").toString());
        return Result.success(service.updateNote(noteId, content, userId));
    }

    // 删除笔记
    @DeleteMapping("delete/{noteId}")
    public Result<Void> deleteNote(
            @PathVariable Long noteId,
            @RequestParam Long userId) {
        service.deleteNote(noteId, userId);
        return Result.success();
    }

    // 获取题目的笔记列表
    @GetMapping("/question/{questionId}")
    public Result<List<StudyNoteDTO>> getQuestionNotes(@PathVariable Long questionId) {
        return Result.success(service.getQuestionNotes(questionId));
    }

    // 分享笔记
    @PostMapping("/{noteId}/share")
    public Result<Void> shareNote(
            @PathVariable Long noteId,
            @RequestParam Long senderId,
            @RequestParam Long receiverId) {
        service.shareNote(noteId, senderId, receiverId);
        return Result.success();
    }

    // 获取分享的笔记详情
    @GetMapping("/shared/{noteId}")
    public Result<StudyNoteDTO> getSharedNote(
            @PathVariable Long noteId,
            @RequestParam Long userId) {
        return Result.success(service.getSharedNote(noteId, userId));
    }

    // 获取用户收到的笔记分享列表
    @GetMapping("/shared/received/{userId}")
    public Result<List<StudyNoteDTO>> getReceivedSharedNotes(@PathVariable Long userId) {
        return Result.success(service.getReceivedSharedNotes(userId));
    }

    // 获取用户分享出去的笔记列表
    @GetMapping("/shared/sent/{userId}")
    public Result<List<StudyNoteDTO>> getSentSharedNotes(@PathVariable Long userId) {
        return Result.success(service.getSentSharedNotes(userId));
    }

    // 管理员：获取所有笔记
    @GetMapping("/admin/all")
    public Result<List<StudyNoteDTO>> getAllNotes(
            @RequestParam Long adminId,
            @RequestParam(required = false) Long userId) {
        return Result.success(service.getAllNotes(adminId, userId));
    }

    @GetMapping("/practice")
    public Result<List<StudyNoteDTO>> getPracticeNotes(
            @RequestParam Long userId,
            @RequestParam Long questionId) {
        // 获取该用户对该题目的所有笔记
        List<StudyNoteDTO> notes = service.getQuestionNotes(questionId).stream()
                .filter(note -> note.getUserId().equals(userId))
                .collect(Collectors.toList());

        // 按照更新时间降序排序
        notes.sort((n1, n2) -> n2.getUpdateTime().compareTo(n1.getUpdateTime()));

        return Result.success(notes);
    }

    // Simplified create/update endpoint for practice page
    @PostMapping("/practice/save")
    public Result<StudyNoteDTO> savePracticeNote(@RequestBody Map<String, Object> params) {
        Long userId = Long.valueOf(params.get("userId").toString());
        Long questionId = Long.valueOf(params.get("questionId").toString());
        String content = (String) params.get("content");
        Long noteId = params.containsKey("noteId") ? Long.valueOf(params.get("noteId").toString()) : null;

        if (noteId != null) {
            return Result.success(service.updateNote(noteId, content, userId));
        } else {
            return Result.success(service.createNote(userId, questionId, content));
        }
    }
} 